<?php

namespace M\Action;
class LoginAction extends BaseAction
{
    public function _initialize()
    {
        parent::_initialize();
    }

    public function index()
    {

        if ($this->visitor->is_login) {
            $this->redirect(U('user/ucenter', '', ''));
        }
        $this->_config_seo(array(
            'title' => '用户登录'
        ));

        $this->display();
    }


    public function fillphone()
    {
        $param = I('post.');
        $this->assign('memberinfo', $param);
        $this->display('User/fillphone');
    }

    public function savephone()
    {
        if (IS_POST) {
            $phone = I('phone', '', 'trim');
            $pwd = md5(I('pwd', '', 'trim'));
            $openid = I('openid', '', 'trim');
            $invocode = I('invocode', '', 'trim');
            $nickname = I('nickname', '', 'trim');
            $mod = M('user');


            $exist = $mod->field('id,password')->where(array(
                'phone' => $phone
            ))->find();
            if ($exist) {
                if ($pwd == $exist['password']) {
                    $data = array(
                        //'phone'=>$phone,
                        'avatar' => I('avatar', '', 'trim'),
                        'openid' => $openid
                    );


                    $track_val = cookie('trackid');

                    if ($invocode || $track_val) {

                        if (!empty($track_val)) {
                            $track = unserialize($track_val);
                            $uid = $track['t'];
                            if ($uid > 0) {
                                $wherex = array(
                                    'id' => $uid
                                );
                            }
                        }

                        if ($invocode) {
                            $wherex = array(
                                'invocode' => $invocode
                            );
                        }
                        $existx = $mod->field('id,fuid,guid')->where($wherex)->find();
                        if ($existx) {
                            $data['fuid'] = $existx['id'];
                            $data['guid'] = $existx['fuid'] ? $existx['fuid'] : 0;
                        } else {
                            $this->ajaxReturn(0, '邀请码不存在或者大小写有误！');
                        }
                    }


                    $res = $mod->where('id=' . $exist['id'])->save($data);
                    $this->visitor->wechatlogin($openid);
                    return $this->ajaxReturn(1, '绑定微信登录成功！');
                } else {
                    return $this->ajaxReturn(0, '输入的账号密码不正确！');
                }
            }

            $info = array(
                'username' => $nickname,
                'phone' => $phone,
                'nickname' => $nickname,
                'password' => $pwd,
                'reg_ip' => get_client_ip(),
                'avatar' => I('avatar', '', 'trim'),
                'state' => 1,
                'status' => 1,
                'reg_time' => time(),
                'last_time' => time(),
                'create_time' => time(),
                'openid' => $openid,
            );
            $res = $mod->add($info);
            $this->visitor->wechatlogin($openid);
            if ($res) {
                return $this->ajaxReturn(1, '微信登录成功！');
            } else {
                return $this->ajaxReturn(0, '微信登录失败！');

            }

        }

    }


    public function register()
    {
        if ($this->visitor->is_login) {
            $this->redirect(U('user/ucenter', '', ''));
        }

        if (IS_POST) {
            $phone = trimall(I('phone', '', 'htmlspecialchars'));

            $username = trimall(I('username', '', 'htmlspecialchars'));

            $email = trimall(I('email', '', 'htmlspecialchars'));

            $password = trimall(I('password', '', 'htmlspecialchars'));

            $repassword = trimall(I('repassword', '', 'htmlspecialchars'));

            $invocode = trimall(I('invocode', '', 'htmlspecialchars'));

            $verify = I('verify', '', 'trim');

            if (!is_numeric($phone)) {
                $this->ajaxReturn(0, '非法操作');
            }

            if ($this->check_verify($verify) === false) {
                $this->ajaxReturn(0, '验证码错误');

            }


            $track_val = cookie('trackid');

            if ($invocode || $track_val) {

                if (!empty($track_val)) {
                    $track = unserialize($track_val);
                    $uid = $track['t'];
                    if ($uid > 0) {
                        $where = array(
                            'id' => $uid
                        );
                    }
                }

                if ($invocode) {
                    $where = array(
                        'invocode' => $invocode
                    );
                }


                $mod = M('user');
                $exist = $mod->field('id,fuid,guid')->where($where)->find();
                if ($exist) {
                    $data['fuid'] = $exist['id'];
                    $data['guid'] = $exist['fuid'] ? $exist['fuid'] : 0;
                } else {
                    $this->ajaxReturn(0, '邀请码不存在或者大小写有误！');
                }

            }

            if ($password != $repassword) {
                $this->ajaxReturn(0, '两次密码输入不一致');
            }

            $res = $this->visitor->register($username, $phone, $email, $password, $data);

            if ($res) {
                return $this->ajaxReturn(1, '注册成功', U('user/ucenter'));
            }

            $this->ajaxReturn(0, $this->visitor->error);
        }


        $this->_config_seo(array(
            'title' => '用户注册'
        ));

        $this->display();
    }


    public function login()
    {

        if ($this->visitor->is_login) {

            $this->redirect(U('user/ucenter', '', ''));
        }

        if (IS_POST) {

            $username = trimall(I('username', '', 'htmlspecialchars'));

            $password = trimall(I('password', '', 'htmlspecialchars'));

            $remember = I('remember', '0', 'trim');

            $verify = I('verify', '', 'trim');

//     if($this->check_verify($verify) === false){
//        $this->ajaxReturn(0, '验证码错误');
//			 
//        }


            $res = $this->visitor->login($username, $password, $remember);

            if ($res) {

                if (empty($callback)) {
                    $callback = U('user/ucenter');
                }

                if (strpos($callback, '/login') !== false) {
                    $callback = U('user/ucenter');
                }

                return $this->ajaxReturn(1, '登录成功', $callback ? $callback : U('user/ucenter'), array(
                    'id' => $this->visitor->get('id'),
                    'nickname' => $this->visitor->get('nickname')
                ));


            }

            $this->ajaxReturn(0, $this->visitor->error);
        }


    }


    public function logout()
    {

        $this->visitor->logout();
        redirect('/');
        exit();

        $this->visitor->logout();
        $callback = $_SERVER['HTTP_REFERER'];
        if (
            strpos($callback, '/login') !== false ||
            strpos($callback, '/user') !== false
        ) {
            $callback = C('yh_headerm_html');
        }

        redirect($callback);
    }


}  